package com.xnx3.wangmarket.Utils;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadPicture {
    //链接url下载图片
    public static void downloadPicture(String urlList,String savaPath) {
        URL url = null;
        int imageNumber = 0;

        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

//            String imageName =  "F:/test.jpg";
            File file = new File(savaPath);
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();

//            if(!file.isDirectory()&&!file.exists()){
//                file.mkdir();
//            }

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
